<template>
  <div class="departments-container">
    <div class="app-container">
      <!-- 卡片组件 -->
      <el-card class="box-card">
        <!-- 使用 Tabs 组件完成标签页布局 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="组织架构" name="first" class="tab-pane">
            <!-- 使用 Layout 布局绘制头部区域 -->
            <el-row type="flex" justify="space-between" align="middle" class="department-header">
              <el-col :span="20">
                <i class="el-icon-s-home" />
                <span class="company">江苏传智播客教育科技股份有限公司</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <el-col>负责人</el-col>
                  <el-col>
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        操作<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addFn('')">添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>

            <!-- 主体区域绘制 -->
            <el-tree class="departments-tree" :data="treeData" :props="defaultProps" :default-expand-all="true">
              <template slot-scope="{ data }">
                <el-row type="flex" justify="space-between" style="height: 50px; width: 100%;" align="middle">
                  <el-col :span="20">
                    <span>{{ data.name }}</span>
                  </el-col>
                  <el-col :span="4">
                    <el-row type="flex" justify="end">
                      <el-col>{{ data.manager }}</el-col>
                      <el-col>
                        <el-dropdown>
                          <span class="el-dropdown-link">
                            操作<i class="el-icon-arrow-down el-icon--right" />
                          </span>
                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item @click.native="addFn(data.id)">添加部门</el-dropdown-item>
                            <el-dropdown-item @click.native="editFn(data.id)">编辑部门</el-dropdown-item>
                            <el-dropdown-item v-if="data && !data.children" @click.native="deleteFn(data.id)">删除部门</el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </template>
            </el-tree>
          </el-tab-pane>
        </el-tabs>

        <!-- 弹框 -->

        <el-dialog
          :title="isEdit ? '编辑部门' : '新增部门'"
          :visible.sync="dialogVisible"
          width="50%"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
        >
          <dept-dialog v-if="dialogVisible" :origin-list="originList" :pid="pid" :is-edit="isEdit" @close="dialogVisible = false" @send="addNew" />
        </el-dialog>

      </el-card>
    </div>
  </div>
</template>

<script>
// 企业部门列表接口,根据id删除对应部门
import { getDepartments, deleteDepartment } from '@/api/departments'
// 导入树状结构函数
import { treeFn } from '@/utils/index'
// 导入编辑子组件
import deptDialog from './deptDialog.vue'
export default {
  name: 'Departments',
  components: {
    deptDialog
  },
  data() {
    return {
      activeName: 'first', // 被激活的 Tab 标签页
      // 树形控件数据
      treeData: [],
      // 配置树形控件展示方式,按照那个属性来渲染的,例如展示的文字
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      dialogVisible: false, // 弹窗的显示与否
      pid: '', // 点击的部门的id值
      isEdit: false, // 是否编辑
      originList: [] // 通过map格式化后的数据
    }
  },
  // 初始化生命周期函数中,调用接口
  created() {
    this.getDepartments()
  },
  methods: {
    // 删除部门
    async deleteFn(id) {
      const resp = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      // 判断Promise对象的返回值,如果点的是取消,不在执行,提示以下信息
      if (resp === 'cancel') return this.$message('已取消删除')
      // 调用删除接口
      const res = await deleteDepartment(id)

      // 提示用户删除成功
      this.$message.success(res.message)
      // 重新加载列表数据
      this.getDepartments()
    },

    // 编辑部门
    editFn(id) {
      // 让弹窗展示,将点击的编辑id赋值给pid
      this.pid = id
      this.dialogVisible = true
      this.isEdit = true
    },

    // 添加部门
    addFn(id) {
      // console.log(id)
      this.pid = id
      this.dialogVisible = true
      this.isEdit = false
    },

    // 子组件通知父亲后,父组件进行关闭弹窗,重新加载
    addNew() {
      this.dialogVisible = false
      this.getDepartments()
    },
    // 定义函数,获取组织架构中企业部门列表
    async getDepartments() {
      const res = await getDepartments()
      // console.log(res.data.depts)
      // this.treeData = res.data.depts
      // 格式化需要传递给子组件的数据
      const originList = res.data.depts.map(item => ({
        code: item.code,
        id: item.id,
        name: item.name,
        pid: item.pid
      }))
      // console.log(originList)
      this.originList = originList
      this.treeData = treeFn(res.data.depts, '')
    },
    // 节点被触发时的事件
    handleNodeClick(data) {
      console.log(data)
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}

.departments-tree {
  margin: 12px 0px;
}

</style>
